import math
from matplotlib.patches import Circle
import matplotlib.pyplot as plt

def calculate_cicular(x,y):
    '''
    已知圆上三点，求圆心坐标。
    X [1,2,3]，x坐标集
    Y [1,2,3], y坐标集
    '''
    x1=x[0]
    x2=x[1]
    x3=x[2]
    y1=y[0]
    y2=y[1]
    y3=y[2]
    a=2*(x2-x1)
    b=2*(y2-y1)
    c=x2*x2+y2*y2-x1*x1-y1*y1
    d=2*(x3-x2)
    e=2*(y3-y2)
    f=x3*x3+y3*y3-x2*x2-y2*y2

    x=(b*f-e*c)/(b*d-e*a)
    y=(d*c-a*f)/(b*d-e*a)
    r=math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))

    return x, y, r

X=[1.5, 0.25, 0]
Y=[-3.0, -3.344772040064913, -3.3541019662496847]
x0,y0,r=calculate_cicular(X,Y)
print(x0,y0,r)


# 画圆
fig = plt.figure()
ax = fig.add_subplot(111)
cir1 = Circle(xy = (x0, y0), radius=r, alpha=0.5) #画圆
ax.add_patch(cir1)
ax.plot(x0, y0, 'ro')
ax.plot(X, Y, 'bo')
plt.axis('scaled')
plt.show()
